package com.sz.biz.csr.service.impl;

import com.sz.biz.csr.constants.CsrConstants;
import com.sz.biz.csr.entity.CsrWorkOrderSchema;
import com.sz.biz.csr.dto.CsrWorkOrderTransactionDto;
import com.sz.biz.csr.service.CsrWorkOrderSchemaService;
import com.sz.biz.csr.service.CsrWorkOrderTransactionService;
import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.ord.service.OrdOrderFeeService;
import com.sz.biz.logistics.ord.service.OrdOrderService;
import com.sz.common.base.constants.AppDomain;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import com.sz.common.core.service.PrincipalUtils;
import com.sz.common.core.system.service.SysFileGroupService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xutao on 17/5/18.
 */
@Service("CsrWorkOrderTransactionService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CsrWorkOrderTransactionServiceImpl extends AbstractService implements CsrWorkOrderTransactionService {

    @Autowired
    CsrWorkOrderSchemaService csrWorkOrderSchemaService;

    @Autowired
    SysFileGroupService fileGroupService;

    @Autowired
    OrdOrderService ordOrderService;
    @Autowired
    OrdOrderFeeService orderFeeService;

    public static int WORK_ORDER_STATUS_TRANSFER = 0;
    public static int WORK_ORDER_STATUS_SUGGEST = 1;
    public static int WORK_ORDER_STATUS_ACCEPT = 2;
    public static int WORK_ORDER_STATUS_DONE = 3;
    public static int WORK_ORDER_STATUS_CLOSED = 4;

    @Override
    protected String getMapperNamespace() {
        return "CsrWorkOrderTransactionMapper";
    }

    @Override
    public int save(CsrWorkOrderTransactionDto entry) {

        int id = 0;

        if (entry != null) {

            checkDataValid(entry);

            entry.setTransactor(PrincipalUtils.getAccountId());

            List<String> attachments = entry.getAttachFileIds();

            if (!CollectionUtils.isEmpty(attachments)) {
                String fileGroupId = fileGroupService.createFileGroup(attachments,
                        "biz.customer.workorder.trasaction", "关联工单ID:" + entry.getRefWorkorderId() + "上传工单附件", AppDomain.OPERATION);
                entry.setFileGroupId(fileGroupId);
            }

            dao.save(getSqlName("insertSelective"), entry);

            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("ADD_CUSTOMERSERVICE_WORKORDER_TRASACTION", entry);
            PrincipalLogUtils.addOperationLog(ModuleNames.CSR, ModuleNames.CSR_WORK_ORDER, UserActions.ADD, "新增工单处理过程", dataBackup);


            CsrWorkOrderSchema csrWorkOrderSchema = new CsrWorkOrderSchema();

            csrWorkOrderSchema.setId(entry.getRefWorkorderId());

            if (entry.getAction() == CsrConstants.WorkOrderAction.CREATE_AND_DELIVER) {

                csrWorkOrderSchema.setStatus(CsrConstants.WorkOrderStatus.CREATED);//已创建

            } else if (entry.getAction() == CsrConstants.WorkOrderAction.END) {

                csrWorkOrderSchema.setStatus(CsrConstants.WorkOrderStatus.RESOLVED);//已处理

            } else if (entry.getAction() == CsrConstants.WorkOrderAction.CLOSE) {

                csrWorkOrderSchema.setStatus(CsrConstants.WorkOrderStatus.CLOSE);//已关闭

            } else {

                csrWorkOrderSchema.setStatus(CsrConstants.WorkOrderStatus.DEALING);//处理中
            }


            csrWorkOrderSchema.setTransactor(entry.getSendTo());

            csrWorkOrderSchemaService.update(csrWorkOrderSchema);
            if (csrWorkOrderSchema.getStatus() == CsrConstants.WorkOrderStatus.CLOSE) {
                CsrWorkOrderSchema currentSchema = csrWorkOrderSchemaService.findById(entry.getRefWorkorderId());
                if (currentSchema != null && currentSchema.getType() == CsrWorkOrderSchemaServiceImpl
                        .WORK_ORDER_SCHEMA_TYPE_AFFECT_WAYBILL) {
                   boolean isOk =  orderFeeService.submitBill(currentSchema.getOrderNo());
                    if(isOk){
                        ordOrderService.sendOutgoingMessage(currentSchema.getOrderNo());
                    }
                }
            }
            id = entry.getId();
        }

        return id;
    }

    @Override
    public List<Integer> findWorkOrderNoByTransactor(int transactor) {

        List<Integer> workorderList = dao.findForList(getSqlName("selectByTrasanctor"), transactor, Integer.class);

        return workorderList;
    }


    private void checkDataValid(CsrWorkOrderTransactionDto csrWorkOrderTransactionDto) {

        if (csrWorkOrderTransactionDto.getAction() < 0 || csrWorkOrderTransactionDto.getAction() == null) {

            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "action");
        }
    }
}
